package web.servlets;

import com.alibaba.druid.sql.parser.Keywords;
import domain.Account;
import domain.Category;
import domain.Diary;
import domain.Product;
import service.CatelogService;
import service.DialogService;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * @projectName: petStore
 * @package: web.servlets
 * @className: searchDialogServlet
 * @author: suqianbei
 * @description: TODO
 * @date: 2021/11/5 1:25
 * @version: 1.0
 */
public class searchDialogServlet extends HttpServlet {
    public static final String VIEW_DIALOG="/WEB-INF/jsp/dialog/Dialog.jsp";
    private  String name;
    private String keywords;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String  keyword=req.getParameter("keywords");
        keywords=keyword;
        keywords="%"+keywords.toLowerCase(Locale.ROOT)+"%";

        name="localhost";
        HttpSession session=req.getSession();
        Account account=(Account)session.getAttribute("account");
        if(account!=null)
        {
            name=account.getUsername();
        }

        DialogService dialogService =new DialogService();
        List<Diary> diaryList=dialogService.searchDiaryList(name,keywords);

        HttpSession httpSession=req.getSession();
        httpSession.setAttribute("name",name);
        httpSession.setAttribute("diaryList",diaryList);
        req.getRequestDispatcher(VIEW_DIALOG).forward(req,resp);
    }
}
